package thesis.ramstorage.util;

import java.awt.Color;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import java.util.SortedMap;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @author Minh Hoang Le
 */
public class ChartRender {

    private Map<Long, NodeParameters> prms;
//	List<Double> ids =  new ArrayList<Double>();
    ChartPanel chartPanel = new ChartPanel(null);

    public ChartRender(String title) {
//		super(title);
    }

    public ChartRender(String title, Map<Long, NodeParameters> prms) {

//        super(title);
        this.prms = prms;
        XYSeries dataSeries = new XYSeries(0); //pass a key for this serie
        for (Long k : prms.keySet()) {
            long l = prms.get(k).getLoad();
            double averageLoadPerNode = l / prms.get(k).getCapacity();
            dataSeries.add((double) k, averageLoadPerNode);
            //replace by our node parameters
        }
        XYSeriesCollection xyDataset = new XYSeriesCollection();
        xyDataset.addSeries(dataSeries);
        JFreeChart chart = createChart(xyDataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 150));
        chartPanel.setVerticalAxisTrace(true);
        chartPanel.setHorizontalAxisTrace(true);
//        setContentPane(chartPanel);

    }

    public JFreeChart createChart(XYSeriesCollection xyDataset) {
        // TODO Auto-generated method stub
        JFreeChart chart =
                ChartFactory.createScatterPlot(
                "Server Statistic", "Node", "Average Load",
                xyDataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);
        return chart;
    }

    private JFreeChart createChart1(XYDataset dataset) {
        final JFreeChart chart = ChartFactory.createXYLineChart(
                "Std Time Line", // chart title
                "X", // x axis label
                "Y", // y axis label
                dataset, // data
                PlotOrientation.VERTICAL,
                true, // include legend
                true, // tooltips
                false // urls
                );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);

//        final StandardLegend legend = (StandardLegend) chart.getLegend();
        //      legend.setDisplaySeriesShapes(true);

        // get a reference to the plot for further customisation...
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        //    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
//        renderer.setSeriesShapesVisible(1, false);
        plot.setRenderer(renderer);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;
    }

    public void chartRender() {
//    	ChartRender demo = new ChartRender("Network Statistic", prms);
//        demo.pack();
//        RefineryUtilities.centerFrameOnScreen(demo);
//        demo.setVisible(true);
    }

    public void update(Observable arg0, Object arg1) {
        // TODO Auto-generated method stub
        //render chart again
//		if (arg1 instanceof Double) {
//			ids.add((Double) arg1);
//		}
//		render("Network Statistic", ids);
//        demo.pack();
//        RefineryUtilities.centerFrameOnScreen(demo);
//        demo.setVisible(true);
    }


    public JFreeChart createTimeSeriesChart(SortedMap<Minute,Double> ids) {
//        XYSeries dataSeries = new XYSeries(0); //pass a key for this serie
//        int i = 0;
//        for (double k : ids) {
//            //replace by our node parameters
//            dataSeries.add(i, k);
//            i++;
//        }
//        XYSeriesCollection xyDataset = new XYSeriesCollection();
//        xyDataset.addSeries(dataSeries);
//        JFreeChart chart = createChart1(xyDataset);

        TimeSeries series = new TimeSeries("Time Line Data", Minute.class);
        for(Minute m : ids.keySet()){
             series.add(m, ids.get(m));
        }
        TimeSeriesCollection dataset = new TimeSeriesCollection(series);
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Time Series",
            "Time",
            "Value",
            dataset,
            true,
            true,
            false
        );
//        ModuloAxis axis = new ModuloAxis("abc", new Range(0d, 20d));
        chart.getXYPlot().getRangeAxis().setUpperBound(20d);
        chart.getXYPlot().getRangeAxis().setLowerBound(0d);
        return chart;
//        chartPanel.setChart(chart);
//        chartPanel.setPreferredSize(new java.awt.Dimension(500, 150));
//        chartPanel.setVerticalAxisTrace(true);
//        chartPanel.setHorizontalAxisTrace(true);
//        setContentPane(chartPanel);
//        this.pack();
//     	RefineryUtilities.centerFrameOnScreen(this);
//     	this.setVisible(true);
    }
    
    public JFreeChart lineChartRender(List<Double> ids) {
        XYSeries dataSeries = new XYSeries(0); //pass a key for this serie
        int i = 0;
        for (double k : ids) {
            //replace by our node parameters
            dataSeries.add(i, k);
            i++;
        }
        XYSeriesCollection xyDataset = new XYSeriesCollection();
        xyDataset.addSeries(dataSeries);
        JFreeChart chart = createChart1(xyDataset);
//        chartPanel.setChart(chart);
//        chartPanel.setPreferredSize(new java.awt.Dimension(500, 150));
//        chartPanel.setVerticalAxisTrace(true);
//        chartPanel.setHorizontalAxisTrace(true);
//        setContentPane(chartPanel);
//        this.pack();
//     	RefineryUtilities.centerFrameOnScreen(this);
//     	this.setVisible(true);
        return chart;
    }
}
